#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "rngs.h"
#include "testhelpers.h"

// this program tests the functionality of the mine card

// Sets a random hand of 50 cards
void setHand(struct gameState *state){
    int i;
    state->handCount[0] = 0;
    for (i = 0; i < 50; i++){
        state->hand[0][i] = randomCard;
        state->handCount[0]++;
    }
    state->hand[0][0] = mine;
    state->hand[0][49] = copper;
    state->hand[0][48] = silver;
}


// The mine card functionality is to trash a copper to gain a silver, or trash a silver to gain a gold
int main(){
    struct gameState *state = malloc(sizeof(struct gameState));
    int i;
    int j;
    int numTests = 20;
    setState(state);
    resetDeck(state);
    setHand(state);
    resetFlag();
    printf("Starting Mine Test\n");

    for (i = 0; i < numTests; i++){
        for (j = 0; j < state->handCount[0]; j++){
            if (state->hand[0][j] == copper){
                cardEffect(mine, j, silver, 0, state, 0, 0);
                    if (state->hand[0][state->handCount[0]-2] != silver){
                        printf("Mine failed to swap copper for silver\n");
                        incState();
                    }
                    if (state->playedCards[0] != mine){
                        assertMessage("Played card is %d, but should be %d\n", state->playedCards[0], mine);
                    }
                break;
            }
        }
        resetDeck(state);
        setHand(state);
        for (j = 0; j < state->handCount[0]; j++){
            if (state->hand[0][j] == silver){
                cardEffect(mine, j, silver, 0, state, 0, 0);
                    if (state->hand[0][state->handCount[0]-2] != gold){
                        printf("Mine failed to swap silver for gold\n");
                        incState();
                    }
                    if (state->playedCards[0] != mine){
                        assertMessage("Played card is %d, but should be %d\n", state->playedCards[0], mine);
                    }
                break;
            }
        }
        resetDeck(state);
        setHand(state);
    }

    if(testState() == 0)
        printf("Mine Card Effect Test Passed\n");
    else
        printf("Mine Card Effect Test Failed %d times\n", testState());

    free(state);
    return 0;

}
